var salaryDistributionDataCheckDetail;
var salaryDistributionCheckData;

 function initial(){

     /**
     JSON.stringify()是将JavaScript对象转换为json字符串
     JSON.parse(jsonstr)是将json字符串转换为javascript对象
     */
     $.ajax({
         url:"http://localhost/PersonManSystem/salary/distribution/query/no/book/"+sessionStorage.XCFFFHthirdinstname+"/待复核.action",//待补充
         type:"get",
         dataType:'json',
         contentType:'application/json;charset=utf-8',
         success:function(data){
             salaryDistributionCheckData = data;

             $("input[name = 'salarydistributionid']").val(sessionStorage.XCFFFHsalarydistributionid);
           $("input[name = 'firstinstname']").val(sessionStorage.XCFFFHfirstinstname);
           $("input[name = 'secondinstname']").val(sessionStorage.XCFFFHsecondinstname);
           $("input[name =  'thirdinstname']").val(sessionStorage.XCFFFHthirdinstname);
           $("input[name = 'personnum']").val(sessionStorage.XCFFFHpersonnum);
           $("input[name = 'basicsalarysum']").val(sessionStorage.XCFFFHbasicsalarysum);

            $(data['data']).each(function(){

                   var tbody = $('<tr style="FONT-WEIGHT: bold; FONT-STYLE: normal; BACKGROUND-COLOR: #eeeeee; TEXT-DECORATION: none"></tr>');

                   tbody.append("<td>"+'<input type="text" name="recordsid" value="' + this.recordsid + '" readonly="readonly" style="border:0;width:100px">'+"</td>");
                   tbody.append("<td>"+'<input type="text" name="clerkname" value="' + this.clerkname + '" readonly="readonly" style="border:0;width:60px">'+"</td>");
                   tbody.append("<td>"+'<input type="text" name="basicsalary" value="' + this.basicsalary + '" readonly="readonly" style="border:0;width:60px">'+"</td>");
                   tbody.append("<td>"+'<input type="text"  name="1'+this.recordsid+'" value="' + this.trafficallowance + '"  style="border:0;width:60px" onblur="update()">'+"</td>" );

                   tbody.append("<td>"+'<input type="text" name="2'+this.recordsid+'" value="' + this.lunchallowance + '"  style="border:0;width:60px">'+"</td>");
                   tbody.append("<td>"+'<input type="text" name="3'+this.recordsid+'" value="' + this.commallowance + '"  style="border:0;width:60px">'+"</td>");
                   tbody.append("<td>"+'<input type="text" name="endowmentins" value="' + this.endowmentins + '" readonly="readonly" style="border:0;width:60px">'+"</td>");
                   tbody.append("<td>"+'<input type="text" name="medicalins" value="' + this.medicalins + '" readonly="readonly" style="border:0;width:60px">'+"</td>");
                   tbody.append("<td>"+'<input type="text" name="uneloymentins" value="' + this.uneloymentins + '" readonly="readonly" style="border:0;width:60px">'+"</td>");
                   tbody.append("<td>"+'<input type="text" name="housingfund" value="' + this.housingfund + '" readonly="readonly" style="border:0;width:60px">'+"</td>");

                   tbody.append("<td>"+'<input type="text" name="4'+this.recordsid+'"  value="'+this.rewardmoney+'">'+"</td>");
                   tbody.append("<td>"+'<input type="text" name="5'+this.recordsid+'"  value="'+this.punishmoney+'">'+"</td>");
                   tbody.append("<td>"+'<input type="text" name="auditstate" value="' + this.auditstate + '" readonly="readonly" style="border:0;width:60px">'+"</td>");






                   tbody.append("<td><input id='"+this.recordsid+"' type='button' name='formsubmit' value='复核' onclick='runqurey(this)'></td>");

                   $("#grid").append(tbody);


               });

         },
         error:function(XMLHttpRequest, textStatus, errorThrown){
             alert("Error");
             alert(XMLHttpRequest.status);
             alert(XMLHttpRequest.readyState);
             alert(textStatus);
         }
     });
 }

//提交数据
  function runqurey(obj){
    var clerkname;
    var thirdinstname;
    var basicsalary;
    var trafficallowance;
    var lunchallowance;
    var commallowance;
    var endowmentins;
    var medicalins;
    var uneloymentins;
    var housingfund;
    var rewardmoney;
    var punishmoney;

    $(salaryDistributionCheckData['data']).each(function(){
        if (this.recordsid == obj.id){
            clerkname = this.clerkname;
            thirdinstname = this.thirdinstname;
            basicsalary = this.basicsalary;
            this.trafficallowance = $("input[name='1"+this.recordsid+"']").val();
            trafficallowance = this.trafficallowance;
            this.lunchallowance = $("input[name='2"+this.recordsid+"']").val();
            lunchallowance = this.lunchallowance;
            this.commallowance = $("input[name='3"+this.recordsid+"']").val();
            commallowance = this.commallowance;
            endowmentins = this.endowmentins;
            medicalins = this.medicalins;
            uneloymentins = this.uneloymentins;
            housingfund = this.housingfund;
            this.rewardmoney = $("input[name='4"+this.recordsid+"']").val();
            rewardmoney = this.rewardmoney;
            this.punishmoney = $("input[name='5"+this.recordsid+"']").val();
            punishmoney = this.punishmoney;
        }
    });

    var salaryDistributionDataCheckDetailInfo = {
        "recordsid": obj.id,
        "clerkname": clerkname,
        "thirdinstname": thirdinstname,
        "basicsalary": basicsalary,
        "trafficallowance": trafficallowance,
        "lunchallowance": lunchallowance,
        "commallowance": commallowance,
        "endowmentins": endowmentins,
        "medicalins": medicalins,
        "uneloymentins": uneloymentins,
        "housingfund": housingfund,
        "rewardmoney": rewardmoney,
        "punishmoney": punishmoney,
        "auditstate": "执行"
    };


     $.ajax({
        url:"http://localhost/PersonManSystem/salary/distribution/submit/update.action",//待补充
        type:"PUT",
        dataType:'json',
        contentType:'application/json;charset=utf-8',
        data:JSON.stringify(salaryDistributionDataCheckDetailInfo),
        success:function(data){
            if (data.msg == '成功') {
                alert("提交成功！");
                window.location='XCFFFH_1.html';
            }

        },
        error:function(XMLHttpRequest, textStatus, errorThrown){
            alert("Error");
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus);
        }
    });

}

function loadBind(){
    // $("input[name = 'formsubmit']").unbind("click");
    // $("input[name = 'formsubmit']").bind("click",function(){

    //     runqurey();
    // });
}